<template>
	<view>
		<!-- 分类框 -->
		<view class="cate-container">
			<!--  -->
			<scroll-view class="cate-left" scroll-y :style="{height:wh + 'px'}">
				<view :class="['cate-left-item', i == active? 'active' : '']" v-for="(item, i) in cateList" :key="i"
					@click="changeActive(i)">
					{{item.cat_name}}
				</view>
			</scroll-view>
			<scroll-view class="cate-right" scroll-y :style="{height:wh + 'px'}" :scroll-top="scrollTop">
				<view class="cate-right-item" v-for="(item,i) in cateListSecond" :key="i">
					<view class="cate-right-title">
						/{{item.cat_name}}/
					</view>
					<!-- 二级分类内容容器 -->
					<view class="cate-right-item-content">
						<!-- 二级分类每一项内容 -->
						<view class="cate-right-item-content-box" v-for="(item2, i2) in item.children" :key="i2"
							@click="gotoGoodsList(item2)">
							<!-- 二级分类图片 -->
							<image :src="item2.cat_icon" mode="widthFix"></image>
							<!-- 二级分类名称 -->
							<text>{{item2.cat_name}}</text>
						</view>
					</view>
				</view>
			</scroll-view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				wh: 0, //窗口高度
				cateList: [], //分类数据
				cateListSecond: [], //二级分类列表
				active: 0,
				scrollTop: 0,
			};
		},
		onLoad() {
			const sysInfo = uni.getSystemInfoSync()
			this.wh = sysInfo.windowHeight
			this.getCateList()
		},

		methods: {
			// 获取分类数据列表
			async getCateList() {
				const {
					data: res
				} = await uni.$http.get('/categories')
				if (res.meta.status == 200) {
					this.cateList = res.message
					this.cateListSecond = res.message[0].children
				}
			},
			// 点击一级分类改变active
			changeActive(i) {
				this.active = i
				this.cateListSecond = this.cateList[i].children
				this.scrollTop = Math.random()
			},
			// 点击跳到商品列表页
			gotoGoodsList(item) {
				uni.navigateTo({
					url: '/subpkg/goods_list/goods_list?cid=' + item.cat_id
				})
			}
		}
	}
</script>

<style lang="scss">
	.cate-container {
		display: flex;

		.cate-left {
			width: 240rpx;
		}

		.cate-left-item {
			display: flex;
			justify-content: center;
			align-items: center;
			height: 120rpx;
			font-size: 24rpx;
			background: #efefef;

			&.active {
				display: flex;
				justify-content: space-between;
				background: #fff;

				&::before {
					content: '';
					width: 6rpx;
					height: 60rpx;
					background-color: #C00000;
				}

				// 右边白线和背景相同
				&::after {
					content: '';
					width: 6rpx;
					height: 60rpx;
					background-color: #fffff;
				}
			}
		}

		.cate-right {
			.cate-right-item {
				.cate-right-title {
					display: flex;
					justify-content: center;
					font-weight: bold;
					font-size: 24rpx;
					padding: 20rpx 0;
				}

				.cate-right-item-content {
					display: flex;
					flex-wrap: wrap;
					background-color: #fff;

					.cate-right-item-content-box {
						width: 33.3%;
						display: flex;
						flex-direction: column;
						justify-content: center;
						align-items: center;
						padding: 20rpx;
						box-sizing: border-box;

						image {
							width: 100%;
						}

						text {
							font-size: 24rpx;
						}
					}
				}
			}
		}
	}
</style>
